package com.his.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.his.common.PageParam;
import com.his.common.Rsp;
import com.his.entity.HisUser;
import com.his.entity.Nursing;
import com.his.entity.NursingCategory;
import com.his.entity.Order;
import com.his.mapper.NursingMapper;
import com.his.mapper.OrderMapper;
import com.his.service.NursingService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Nursing)表服务实现类
 *
 * @author makejava
 * @since 2020-10-23 18:13:29
 */
@Service("nursingService")
@Transactional()
public class NursingServiceImpl implements NursingService {
    @Resource
    private NursingMapper nursingMapper;
    @Resource
    private OrderMapper orderMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Nursing queryById(Integer id) {
        return this.nursingMapper.queryById(id);
    }

    /**
     * 查询所有通过实体类属性
     *
     * @param pageParam 分页实例对象
     * @return 查询的结果
     */
    @Override
    public Rsp queryAll(PageParam<Nursing> pageParam, HisUser hisUser) {
        PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());
        //排序
        for (int i = 0; i < pageParam.getOrderParams().length; i++) {
            PageHelper.orderBy(pageParam.getOrderParams()[i]);
        }
        Nursing model = pageParam.getModel();
        if (hisUser.getRoles().contains("nurse")){
            model.setNurseId(hisUser.getId());
        }
        List<Nursing> list = this.nursingMapper.queryAll(model);
        return Rsp.success(new PageInfo<>(list));
    }

    /**
     * 新增数据
     *
     * @param nursing 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(Nursing nursing) {
        return nursingMapper.insert(nursing)>0;
    }

    /**
     * 修改数据
     *
     * @param nursing 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(Nursing nursing) {
        Order order=new Order();
        order.setCategoryId(3);
        order.setCreateUserId(nursing.getNurseId());
        order.setPatientId(nursing.getPatientId());
        //查询护理的类别和金额
        NursingCategory category = nursingMapper.getNursingCategory(nursing.getNursingCategoryId());
        order.setDescript(category.getNursingName());
        order.setTotalMoney(category.getNursingPrice());
        orderMapper.insert(order);
        nursing.setNursingTime(new Date());
        nursing.setNursingStatus("1");
        return this.nursingMapper.update(nursing)>0;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.nursingMapper.deleteById(id) > 0;
    }
}